package org.dromara.commission.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.dromara.commission.domain.LzshNetWorkLatitude;
import org.dromara.commission.domain.bo.LzshNetWorkLatitudeBo;
import org.dromara.commission.domain.vo.LzshNetWorkLatitudeVo;
import org.dromara.commission.mapper.LzshNetWorkLatitudeMapper;
import org.dromara.commission.service.ILzshNetWorkLatitudeService;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 网点纬度Service业务层处理
 *
 * @author sxl
 * @date 2024-07-25
 */
@RequiredArgsConstructor
@Service
public class LzshNetWorkLatitudeServiceImpl implements ILzshNetWorkLatitudeService {

    private static final int BATCH_SIZE = 20;
    // 定义Redis操作的批处理大小
    private static final int REDIS_BATCH_SIZE = 1000;
    private final LzshNetWorkLatitudeMapper baseMapper;
    @Resource
    private RedisTemplate<String, List<LzshNetWorkLatitude>> redisTemplate;

    @Autowired
    private RedissonClient redissonClient;
    /**
     * 查询网点纬度
     *
     * @param id 主键
     * @return 网点纬度
     */
    @Override
    public LzshNetWorkLatitudeVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询网点纬度列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 网点纬度分页列表
     */
    @Override
    public TableDataInfo<LzshNetWorkLatitudeVo> queryPageList(LzshNetWorkLatitudeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<LzshNetWorkLatitude> lqw = buildQueryWrapper(bo);
        if(!LoginHelper.isSuperAdmin()){
            bo.setBusinessCode(LoginHelper.getUsername());
            lqw = buildQueryWrapper(bo);
        }
        Page<LzshNetWorkLatitudeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的网点纬度列表
     *
     * @param bo 查询条件
     * @return 网点纬度列表
     */
    @Override
    public List<LzshNetWorkLatitudeVo> queryList(LzshNetWorkLatitudeBo bo) {
        //如果是管理员则导出全部，如果是非管理员只能导出自己对应的数据
        if(!LoginHelper.isSuperAdmin()){
            bo.setBusinessCode(LoginHelper.getUsername());
        }
        LambdaQueryWrapper<LzshNetWorkLatitude> lqw = buildQueryWrapper(bo);


        return baseMapper.selectVoList(lqw);
    }

    @Override
    public LzshNetWorkLatitudeVo queryByBusinessCode(String businessCode, String agentName, String accountPeriod) {
          //使用Wrapper查询符合businessCode和agentName和accountPeriod的一条数据
        return baseMapper.selectVoByBusinessCode(businessCode, agentName, accountPeriod);
    }

    private LambdaQueryWrapper<LzshNetWorkLatitude> buildQueryWrapper(LzshNetWorkLatitudeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<LzshNetWorkLatitude> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getAccountPeriod()), LzshNetWorkLatitude::getAccountPeriod, bo.getAccountPeriod());
        lqw.like(StringUtils.isNotBlank(bo.getBusinessCode()), LzshNetWorkLatitude::getBusinessCode, bo.getBusinessCode());
        lqw.like(StringUtils.isNotBlank(bo.getAgentName()), LzshNetWorkLatitude::getAgentName, bo.getAgentName());
        lqw.like(StringUtils.isNotBlank(bo.getChannel()), LzshNetWorkLatitude::getChannel, bo.getChannel());
        lqw.like(StringUtils.isNotBlank(bo.getShopCode()), LzshNetWorkLatitude::getShopCode, bo.getShopCode());
        lqw.like(StringUtils.isNotBlank(bo.getNetworkName()), LzshNetWorkLatitude::getNetworkName, bo.getNetworkName());
        lqw.eq(bo.getOriginalAmount() != null, LzshNetWorkLatitude::getOriginalAmount, bo.getOriginalAmount());
        lqw.eq(bo.getAdjustAmount() != null, LzshNetWorkLatitude::getAdjustAmount, bo.getAdjustAmount());
        lqw.eq(bo.getTotalAmount() != null, LzshNetWorkLatitude::getTotalAmount, bo.getTotalAmount());
        return lqw;
    }

    /**
     * 新增网点纬度
     *
     * @param bo 网点纬度
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(LzshNetWorkLatitudeBo bo) {
        LzshNetWorkLatitude add = MapstructUtils.convert(bo, LzshNetWorkLatitude.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改网点纬度
     *
     * @param bo 网点纬度
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(LzshNetWorkLatitudeBo bo) {
        LzshNetWorkLatitude update = MapstructUtils.convert(bo, LzshNetWorkLatitude.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(LzshNetWorkLatitude entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除网点纬度信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }


    @Override
    public Boolean deleteByBcAndAn(String businessCode, String agentName) {
        return baseMapper.deleteByBcAndAn(businessCode, agentName);
    }


    @Transactional(rollbackFor = Exception.class)
    public void saveBatch(List<LzshNetWorkLatitude> dataList) {
        try {
            baseMapper.insertBatch(dataList);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    // 序列化方法
    private byte[] serialize(LzshNetWorkLatitude obj) {
        return obj.toString().getBytes();
    }

}


